package com.lanou.pianke.piece.web.pieceService.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.lanou.pianke.piece.mapper.PieceMapper;
import com.lanou.pianke.piece.web.controller.PieceController;
import com.lanou.pianke.piece.web.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class PieceService implements com.lanou.pianke.piece.web.pieceService.PieceService {
    @Autowired
    private PieceMapper pieceMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public List<Piece> selectOneTagPieceByTagid(Integer tagId) {
        return pieceMapper.selectOneTagPieceByTagid(tagId);
    }

    @Override
    public List<Piece> selectAllPiece(@RequestParam(defaultValue = "1") int pageNum,
                                      @RequestParam(defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return pieceMapper.selectAllPiece();
    }

    @Override
    public Integer addPiece(Piece piece) {
        long addtime = System.currentTimeMillis();
        String s = Integer.toHexString((int) addtime);
        String s1 = Long.toHexString(piece.getUid());
        piece.setPieceid(s+s1);
        piece.setPieceaddtime(addtime);
        return pieceMapper.addPiece(piece);
    }

    @Override
    public Integer selectOnePieceByPid(String pieceId,Long uid) {
        Piece piece = pieceMapper.selectOnePieceByPid(pieceId);
        Long uid2 = piece.getUid();
        if (uid.equals(uid2)){
            return 0;
        }else {
            Visitor visitor = new Visitor(uid2,uid);
            pieceMapper.insertVisitor(visitor);
            return 1;
        }

    }

    @Override
    public List<Pcomment> selectCommentByPieceId(String pieceId) {
        List<Pcomment> pcomment = pieceMapper.selectCommentByPieceId(pieceId);
        return pcomment;
    }

    @Override
    public List<Preplay> selectPreplayByPcommentId(String pcomment_id) {
        return pieceMapper.selectByPcommentId(pcomment_id);
    }

    @Override
    public Integer addPieceComment(String text,String pieceid,Long uid,String pieceText) {
        Long pieceUid = pieceMapper.selectPieceUidByPieceText(pieceText);
        Long time = new Date().getTime();//碎片评论产生时间
        Pcomment pcomment = new Pcomment(pieceid,time,text,uid,pieceUid);
        return pieceMapper.addPieceComment(pcomment);
    }


    @Override
    public Integer insertPreplay(String commentText,String replayText,Long uid) {
        Long time = new Date().getTime();
        Pcomment pcomment = pieceMapper.selectPcommentIdAndUidBycommentText(commentText);
        Long pcomId = pcomment.getId();
        Long pcomuid = pcomment.getUid();
        Preplay preplay = new Preplay(replayText,time,pcomId,uid,pcomuid);
        Integer integer = pieceMapper.insertPreplay(preplay);
        return integer;
    }

    @Override
    public void selectPiece() {
        pieceMapper.selectPiece();
    }

    @Override
    public void deleteReplayByReplayText(String replayText) {
        pieceMapper.deleteReplayByReplayText(replayText);
    }

    @Override
    public void delPieceCommentBycommentText(String commentText) {
        pieceMapper.delPieceCommentBycommentText(commentText);
    }

    @Override
    public void insertPieceLike(String pieceId,Long uid) {
        Integer isreaded = 0;
        PieceIsLike isLike = new PieceIsLike(pieceId,uid,isreaded);
        pieceMapper.insertPieceLike(isLike);
    }

    @Override
    public Piece selectOnePieceByPieceId(String pieceId) {
        return pieceMapper.selectOnePieceByPieceId(pieceId);
    }

    @Override
    public List<Long> selectActiveUser() {
        Long time = (new Date().getTime())/1000 - 2590000;
        return pieceMapper.selectActiveUser(time);
    }

    @Override
    public void addRedis() {
        List<Long> longs = activeUsers();
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        for (int i = 0; i < longs.size(); i++) {
            zSetOperations.add("activeUsers",longs.get(i),12);
        }
    }

    @Override
    public void getRedis() {
        Set userActive1 = redisTemplate.opsForZSet().range("userActive", 0, -1);
            List userlist = new ArrayList();
            for (Object o : userActive1) {
                userlist.add(o);
            }
    }

    //查询活跃用户
    public List<Long> activeUsers(){
        List<Long> longs = selectActiveUser();
        return longs;
    }


}
